﻿@model CafeOnline.Domain.Entities.ProductGroup
@using CafeOnline.Localization;

<div class="span9">
    <div class="box">
        <div class="title">
            <h4>
                <span>Create Product Group</span>
            </h4>
        </div>
        <div class="content">
            <div class="form-row row-fluid notify" style="display:none;" id="resultDiv"></div>
            <form action="forms-validation.html" id="form-validate" class="form-horizontal" novalidate="novalidate">
                <div class="form-row row-fluid">
                    <div class="span11">
                        <div class="row-fluid">
                            <label for="required" class="form-label span4">Name</label>
                            <input id="ip_ProductGroupName" type="text" placeholder="Enter your product group name" name="required" id="required" class="span8 text">
                        </div>
                    </div>
                </div>
                <div class="form-row row-fluid">
                    <div class="span11">
                        <div class="row-fluid">
                            <label for="required" class="form-label span4">Description</label>
                            <input id="ip_Description" type="text" placeholder="Enter your description" name="required" id="required" class="span8 text">
                        </div>
                    </div>
                </div>
                <div class="form-row row-fluid">
                    <div class="span11">
                        <div class="row-fluid">
                            <label for="combobox" class="form-label span4">Parent Product Group</label>
                            <div class="span7 controls marginR10">
                                @Html.DropDownList("ProductGroupId", String.Empty)

                            </div>
                            <button class="btn span1" type="button" id="btn_AddProductGroupParent"><span class="icon-plus"></span></button>
                        </div>
                    </div>
                </div>

                <div class="form-row row-fluid">
                    <div class="span12">
                        <div class="row-fluid">
                            <div class="form-actions">
                                <div class="span4"></div>
                                <div class="span8 controls">
                                    <button id="btn_Save" class="btn marginR10 btn-info" type="submit">Save & Add New</button>
                                    <button id="btnSaveClose" class="btn marginR10 btn-info" type="submit">Save And Close</button>
                                    <button id="btn_Cancel" class="btn">Cancel</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </form>

        </div>

    </div>
    <!-- End .box -->
</div>

<div class="span3">

    @Html.Partial("_UploadImage")

</div>

<script type="text/javascript">
    $(function () {
        btn_Save_Click();
        btn_Cancel_Click();
    });

    function btn_Save_Click() {
        $('#btn_Save').click(function (e) {
            e.preventDefault();
            uploadImageAndSubmitForm(addItem);
            $('.fileupload-exists').trigger('click');
        });
        $('#btnSaveClose').click(function (e) {
            e.preventDefault();
            uploadImageAndSubmitForm(editItem);
        });
        
    }
    function addItem(imgSrc) {
        addNewItem(true, imgSrc);
    }
    function editItem(imgSrc) {
        addNewItem(false, imgSrc);
    }
    function addNewItem(addNew, imgSrc) {
        var objProductGroup = {
            ProductGroupName: $('#ip_ProductGroupName').val(),
            ProductGroupDesc: $('#ip_Description').val(),
            //parent: $('#selector_ProductGroupParentId').selected(),
            ImageUrl: imgSrc,
        };
        $.ajax({
            url: '@Url.Action("Save", "ProductGroup")',
                data: JSON.stringify(objProductGroup),
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',

                success: function (data) {
                    if (addNew) {
                        $('#ip_ProductGroupName').val('');
                        $('#ip_Description').val('');
                        $('#resultDiv').html(data);
                        $('#resultDiv').show();
                    } else {
                        backToIndex();
                    }
                },
                error: function () {
                    alert("fail");
                },
        });
    }

    function btn_Cancel_Click() {
        $('#btn_Cancel').click(function (e) {
            e.preventDefault();
            backToIndex();
        });
    }

    function backToIndex() {
        $('#divDetail').show();
        $('#actionDiv').show();
        $('#searchDiv').hide();
        $('#category-grid').trigger('reloadGrid');
    }
</script>

